package com.heima.common.exception;

import com.heima.model.admin.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Description: 目的是给用户提供友好的提示信息
 * @Version: V1.0
 */
@Slf4j
@RestControllerAdvice   // Springmvc 异常处理拦截注解
public class ExceptionCatch {


    @ExceptionHandler(Exception.class)
    public ResponseResult exceptionHandler(Exception e) {
        e.printStackTrace ();
        log.error ( "统一捕获异常：{}", e );
        return ResponseResult.errorResult ( AppHttpCodeEnum.SERVER_ERROR, "服务器出现异常，请稍后重试" );
    }


    /**
     * 拦截自定义异常
     *
     * @return
     */
    @ExceptionHandler(CustomException.class)
    public ResponseResult custException(CustomException ex) {
        ex.printStackTrace ();
        log.error ( "CustomException ex:{}", ex );
        AppHttpCodeEnum codeEnum = ex.getAppHttpCodeEnum ();
        return ResponseResult.errorResult ( codeEnum );
    }
}

